Micron Document




JavaScript syntax
part 39/59 · 107.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
console.log(a || b); // if a is true, return a, otherwise return b
console.log(a && b); // if a is false, return a, otherwise return b

Programmers who are more familiar with the behavior in C might find this feature surprising, but it allows for a more concise expression of patterns like null coalescing:

const s = t || "(default)"; // assigns t, or the default value, if t is null, empty, etc.

Logical assignment

| ??= | Nullish assignment |
|---|---|
| //= | Logical Or assignment |
| &&= | Logical And assignment |

Bitwise

JavaScript supports the following binary bitwise operators:

| & | AND |
|---|---|
| / | OR |
| ^ | XOR |
| ! | NOT |
| << | shift left (zero fill at right) |
| >> | shift right (sign-propagating); copies of the leftmost bit (sign bit) are shifted in from the left |
| >>> | shift right (zero fill at left). For positive numbers, >> and >>> yield the same result. |

Examples:

const x = 11 & 6;
console.log(x); // 2

JavaScript supports the following unary bitwise operator:

Bitwise Assignment

JavaScript supports the following binary assignment operators:

| &= | and |
|---|---|
| /= | or |
| ^= | xor |
| <<= | shift left (zero fill at right) |
| >>= | shift right (sign-propagating); copies of the leftmost bit (sign bit) are shifted in from the left |
| >>>= | shift right (zero fill at left). For positive numbers, >>= and >>>= yield the same result. |

Examples:

let x=7;
console.log(x); // 7
x<<=3;
console.log(x); // 7->14->28->56

String

| = | assignment |
|---|---|
| + | concatenation |
| += | concatenate and assign |

Examples:

let str = "ab" + "cd"; // "abcd"
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────